//
//  LS_FindViewController.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/4.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_FindViewController.h"
#import "LS_FindViewModel.h"
#import "LS_SearchTextfield.h"
#import "LS_TabBindViewHelp.h"
#import "LS_TableView.h"
#import "LS_HeadViewTableView.h"
@interface LS_FindViewController ()
@property(nonatomic,strong)LS_FindViewModel *viewModel;
@property(nonatomic,strong)HJLayoutBtn *nav_leftButton;
@property(nonatomic,strong)LS_SearchTextfield *nav_textfield;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,strong)LS_TabBindViewHelp *tableBindViewHelp;
@property(nonatomic,strong)LS_TableView *tableView;
@property(nonatomic,strong)LS_HeadViewTableView *headerView;
@end

@implementation LS_FindViewController
-(instancetype)initWithModel:(LS_FindViewModel *)ViewModel
{
    if (self = [super init]) {
        self.viewModel = ViewModel;
    }return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
    self.page = 1;
    [self bindView];
    
}
-(void)bindView
{
   
    self.viewModel.MyVc = self;
    self.nav_leftButton.rac_command = self.viewModel.leftNavitemCommand;
    //用来获取最新发送的信号,只能用于信号中的信号
    [self.viewModel.leftNavitemCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        NSLog(@"点击了定位");
    }];
    //请求数据
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    param[@"page"] = @(self.page);
    param[@"pagesize"] = @(10);
    param[@"city"] = @"南昌市";
    [self.viewModel.foundCommand execute:param];
    
//    self.headerView.bannerData = RACObserve(self.viewModel, bannerArr);
//    self.headerView.newsData = RACObserve(self.viewModel, newsArr);
//    self.headerView.headData = RACObserve(self.viewModel, foundDataDic);
    [self.headerView ls_headViewSignal:RACObserve(self.viewModel, foundDataDic) WithBackModel:self.viewModel];
    self.tableBindViewHelp = [LS_TabBindViewHelp bindingHelperForTableView:self.tableView sourceSignal:RACObserve(self.viewModel, foundDataDic) selectCommand:nil templateCell:@[@"LS_SoreItemTableViewcell",@"LS_PublishAdTableViewCell",@"LS_AdvertiseItemsTableViewCell"] withViewModel:self.viewModel];
  
}
-(void)setNav
{
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:self.nav_leftButton];
    self.navigationItem.leftBarButtonItem = leftItem;
 
    
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    [navView addSubview:self.nav_textfield];
    self.navigationItem.titleView =navView;
}
#pragma mark -
#pragma mark -- lazy
-(HJLayoutBtn *)nav_leftButton
{
    return LS_LAZY(_nav_leftButton, ({
        HJLayoutBtn *button = [[HJLayoutBtn alloc]init];
        button.HJ_Spacing = 5;
        button.HJ_Style = HJLaoutBtnStyleImageRight;
        button.frame = CGRectMake(0, 0, 40, 40);
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        [button setImage:LS_Getimage(@"down_arrow") forState:UIControlStateNormal];
        [button setTitle:@"南昌市" forState:UIControlStateNormal];
        button;
    }));
}
-(LS_SearchTextfield *)nav_textfield
{
    return LS_LAZY(_nav_textfield, ({
        LS_SearchTextfield *textfield = [[LS_SearchTextfield alloc]initWithFrame:CGRectMake(0, 7, ScreenWidth-100, 30)];
        textfield.isEdit = YES;
       textfield;
    }));
}


-(LS_TableView *)tableView
{
    return LS_LAZY(_tableView, ({
        LS_TableView *tableview = [[LS_TableView alloc]initWithFrame:self.view.bounds];
        tableview.tableHeaderView = self.headerView;
//      tableview.rowHeight = 180;
        [self.view addSubview:tableview];
        tableview;
    }));
}
-(LS_HeadViewTableView *)headerView
{
    return LS_LAZY(_headerView, ({
        LS_HeadViewTableView *headView = [[LS_HeadViewTableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 200 + 100)];
        headView;
    }));
}
@end
